const app = getApp();
const util = require("../../utils/util.js");
const form = require("../../utils/form.js");
const region = require('../../utils/region.js');

Page({
  data: {
    taskId: "",
    formId: "",
    baseUrl: form.baseUrl,
    task: form.task,
    itemData: form.itemData,
    itemRender: form.itemRender,
    itemList: form.itemList,
    customData: form.customData,
    customRender: form.customRender,
    customItemList: form.customList,
  },
  //事件处理函数
  bindViewTap() {
    app.process(this, e);
  },

  // 页面加载
  onLoad(options) {
    let taskId = null;
    let formId = null;

    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      let result = scene.split(",");
      taskId = result[0];
      formId = result[1];
    } else {
      taskId = options.taskId;
      formId = options.formId;
    }

    this.setData({
      taskId: taskId,
      formId: formId
    });
    form.id = formId;

    let _this = this;
    util.request("/api/task/getFormItems", {
      taskId: this.data.taskId,
      formId: this.data.formId
    }, function (res) {
      let data = res.data.data;
      if (data) {
        form.loadData(data);
        _this.setData({
          taskId: form.task.id,
          task: form.task,
          itemData: form.itemData,
          itemRender: form.itemRender,
          itemList: form.itemList,
          customData: form.customData,
          customRender: form.customRender,
          customItemList: form.customList
        });
      }
    });
  },

  onShow() {

  },

  // 监听值变更事件
  listenerValue(e) {
    let name = e.target.id;
    let value = e.detail.value;
    form.updateData(name, value);
    this.data.itemRender[name] = value;
    this.setData({
      itemRender: this.data.itemRender
    });
  },

  // custom监听值变更事件
  listenerCustomValue(e) {
    let name = e.target.id;
    let value = e.detail.value;
    form.updateData(name, value);
    this.data.customRender[name] = value;
    this.setData({
      customRender: this.data.customRender
    });
  },

  // 单选
  listenerRadioValue(e) {
    let name = e.target.id;
    let value = e.detail.value;
    this.data.itemRender[name].radioValue = value;
    this.data.itemRender[name].checkShowText();
    this.setData({
      itemRender: this.data.itemRender
    });
    form.updateData(name, this.data.itemRender[name].getData());
  },

  // custom单选
  listenerCustomRadioValue(e) {
    let name = e.target.id;
    console.log(name)
    let value = e.detail.value;
    console.log(value)
    this.data.customRender[name].radioValue = value;
    this.data.customRender[name].checkShowText();
    this.setData({
      customRender: this.data.customRender
    });
    form.updateData(name, this.data.customRender[name].getData());
  },

  // 单选下的text
  listenerRadioTextValue(e) {
    let name = e.target.id;
    let value = e.detail.value;
    if (this.data.itemRender[name].showText) {
      this.data.itemRender[name].textValue = value;
    }
    this.setData({
      itemRender: this.data.itemRender
    });
    form.updateData(name, this.data.itemRender[name].getData());
  },

  // custom单选下的text
  listenerCustomRadioTextValue(e) {
    let name = e.target.id;
    let value = e.detail.value;
    if (this.data.customRender[name].showText) {
      this.data.customRender[name].textValue = value;
    }
    this.setData({
      customRender: this.data.customRender
    });
    form.updateData(name, this.data.customRender[name].getData());
  },

  // 复选框组件
  listenerCheckBoxValue(e) {
    let name = e.target.id;
    let value = e.detail.value;
    form.updateData(name, value.join(','));
    for (var j in this.data.itemRender[name]) {
      if (value.indexOf(j) != -1) {
        this.data.itemRender[name][j] = true;
      } else {
        this.data.itemRender[name][j] = false;
      }
    }
    this.setData({
      itemRender: this.data.itemRender
    })
  },

  // custom复选框组件
  listenerCustomCheckBoxValue(e) {
    let name = e.target.id;
    let value = e.detail.value;
    form.updateData(name, value.join(','));
    for (var j in this.data.customRender[name]) {
      if (value.indexOf(j) != -1) {
        this.data.customRender[name][j] = true;
      } else {
        this.data.customRender[name][j] = false;
      }
    }
    this.setData({
      customRender: this.data.customRender
    })
  },

  // 监听三级联动的滚动切换
  listenerRegionColumn(e) {
    let name = e.target.id;
    let column = e.detail.column;
    let columnValue = e.detail.value;
    // 如果是省份滚动了
    if (column == 0) {
      let reg = region.getRegionByIndex(columnValue, 0, 0);
      this.data.itemRender[name].data = reg.data;
      this.data.itemRender[name].index = reg.index;
      this.data.itemRender[name].code = reg.code;
      this.setData({
        itemRender: this.data.itemRender
      });
    }

    // 如果是市滚动了
    if (column == 1) {
      let reg = region.getRegionByIndex(this.data.itemRender[name].index[0], columnValue, 0);
      this.data.itemRender[name].data = reg.data;
      this.data.itemRender[name].index = reg.index;
      this.data.itemRender[name].code = reg.code;
      this.setData({
        itemRender: this.data.itemRender
      });
    }
  },

  // 监听三级联动的变化
  listenerRegionChange(e) {
    let name = e.target.id;
    let value = e.detail.value;
    let regionData = form.itemData[name].split(",");
    let reg = region.getRegionByIndex(value[0], value[1], value[2]);
    form.updateData(name, reg.code.join(",") + "," + regionData[3]);
  },

  // 监听详细地址的变化
  listenerRegionText(e) {
    let name = e.target.id;
    let value = e.detail.value;
    let regionData = form.itemData[name].split(",");
    regionData[3] = value;
    this.data.itemRender[name].text = value;
    form.updateData(name, regionData.join(","));
    this.setData({
      itemRender: this.data.itemRender
    })
  },

  // 选择图片
  chooseImage(e) {
    let name = e.currentTarget.id;
    let taskId = form.task.id;
    let formId = form.id;
    let _this = this;
    _this.data.refresh = false;
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        if (_this.data.itemRender[name].length + res.tempFilePaths.length > 9) {
          util.toast("最多上传9张照片");
          return false;
        } else {
          for (let file in res.tempFilePaths) {
            util.uploadImg("/api/task/addPicture", res.tempFilePaths[file], {
              formId: formId,
              taskId: taskId,
              name: name
            }, function (res) {
              let result = JSON.parse(res.data);
              _this.data.itemRender[name].push([form.baseUrl, result.data[0]]);
              // 更新form的数据
              var imgArr = [];
              for (var i in _this.data.itemRender[name]) {
                imgArr.push(_this.data.itemRender[name][i][1])
              }
              form.updateData(name, imgArr.join(','))
              // 渲染上传成功的图片到页面
              _this.setData({
                itemRender: _this.data.itemRender
              });
            });
          }
        }
      }
    });
  },

  deleteImage(e) {
    let data = e.currentTarget.id.split(',');
    let index = data[0];
    let baseUrl = data[1] == form.baseUrl;
    let image = data[2];
    let name = data[3];
    let img = image.replace(form.baseUrl, "");
    let formId = form.id;
    let taskId = form.task.id;
    let _this = this;
    if (baseUrl) {
      util.request("/api/task/deletePicture", {
        formId: formId,
        name: name,
        taskId: taskId,
        image: image,
      }, function (res) {
        if (index > -1) {
          _this.data.itemRender[name].splice(index, 1);
        }
        // 更新form的数据
        form.updateData(name, _this.data.itemRender[name].join(','))
        _this.setData({
          itemRender: _this.data.itemRender
        });
      }, {
          loading: false
        });
    } else {
      if (index > -1) {
        _this.data.itemRender[name].splice(index, 1);
      }
      _this.setData({
        itemRender: _this.data.itemRender
      });
    }
  },

  star1(e) {
    let name = e.currentTarget.id.split(',')[0];
    this.data.itemRender[name] = 1;
    this.setData({
      itemRender: this.data.itemRender
    });
    form.updateData(name, 1);
    util.request("/api/task/updateItemData", {
      taskId: form.task.id,
      formId: form.id,
      name: name,
      random_code: form.random_code,
      value: 1
    }, function () { }, {
        loading: false
      });
  },
  star2(e) {
    let name = e.currentTarget.id.split(',')[0];
    this.data.itemRender[name] = 2;
    this.setData({
      itemRender: this.data.itemRender
    });
    form.updateData(name, 2);
    util.request("/api/task/updateItemData", {
      taskId: form.task.id,
      formId: form.id,
      name: name,
      random_code: form.random_code,
      value: 2
    }, function () { }, {
        loading: false
      });
  },
  star3(e) {
    let name = e.currentTarget.id.split(',')[0];
    this.data.itemRender[name] = 3;
    this.setData({
      itemRender: this.data.itemRender
    });
    form.updateData(name, 3);
    util.request("/api/task/updateItemData", {
      taskId: form.task.id,
      formId: form.id,
      name: name,
      random_code: form.random_code,
      value: 3
    }, function () { }, {
        loading: false
      });
  },
  star4(e) {
    let name = e.currentTarget.id.split(',')[0];
    this.data.itemRender[name] = 4;
    this.setData({
      itemRender: this.data.itemRender
    });
    form.updateData(name, 4);
    util.request("/api/task/updateItemData", {
      taskId: form.task.id,
      formId: form.id,
      name: name,
      random_code: form.random_code,
      value: 4
    }, function () { }, {
        loading: false
      });
  },
  star5(e) {
    let name = e.currentTarget.id.split(',')[0];
    this.data.itemRender[name] = 5;
    this.setData({
      itemRender: this.data.itemRender
    });
    form.updateData(name, 5);
    util.request("/api/task/updateItemData", {
      taskId: form.task.id,
      formId: form.id,
      name: name,
      random_code: form.random_code,
      value: 5
    }, function () { }, {
        loading: false
      });
  },

  // 保存form表单
  saveForm() {
    util.request("/api/task/saveItemData", {
      taskId: form.task.id,
      formId: form.id,
      itemData: form.getFormData(),
      customData: form.getFormCustomData()
    }, function (res) {
      wx.switchTab({
        url: "/pages/task/index",
      })
    });
  },

  // 分享
  onShareAppMessage: (res) => {
    if (res.from === 'button') {
      console.log("来自页面内转发按钮");
      console.log(res.target);
    } else {
      console.log("来自右上角转发菜单")
    }
    return {
      title: '分享标题',
      path: '分享出去后别人打开的页面路径',
      imageUrl: "分享出去别人看到的图片",
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }
  }
})